﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Avalon.Windows.Controls;

using Supremacy.Text;
using System.Globalization;
using Supremacy.Resources;
using Supremacy.UI;

namespace Supremacy.Editor
{
    /// <summary>
    /// Interaction logic for LocalizedTextEditor.xaml
    /// </summary>
    public partial class LocalizedTextEditor : Window
    {
        private readonly LocalizedTextGroup _textGroup;

        public LocalizedTextEditor(LocalizedTextGroup textGroup)
        {
            if (textGroup == null)
                throw new ArgumentNullException("textGroup");
         
            _textGroup = textGroup;
            
            InitializeComponent();

            this.Owner = EditorApp.Current.MainWindow;
            this.DataContext = _textGroup;

            _textGroup.ListChanged += new ListChangedEventHandler(TextGroup_ListChanged);

            UpdateCultureList();
        }

        private void TextGroup_ListChanged(object sender, ListChangedEventArgs e)
        {
            if ((e.ListChangedType == ListChangedType.ItemAdded)
                || (e.ListChangedType == ListChangedType.ItemDeleted)
                || (e.ListChangedType == ListChangedType.Reset))
            {
                UpdateCultureList();
            }
        }

        private void UpdateCultureList()
        {
            this.CultureList.ItemsSource = CultureInfo.GetCultures(CultureTypes.NeutralCultures).Where(
                o => !(from i in _textGroup select CultureInfo.GetCultureInfo(i.Culture)).Contains(o) && !String.IsNullOrEmpty(o.Name));
            this.CultureList.Items.SortDescriptions.Add(new SortDescription("DisplayName", ListSortDirection.Ascending));
            if (this.CultureList.Items.Count > 0)
                this.CultureList.SelectedIndex = 0;
        }

        public LocalizedTextGroup TextGroup
        {
            get { return _textGroup; }
        }

        private void AddCultureButton_Click(object sender, RoutedEventArgs e)
        {
            CultureInfo culture = CultureList.SelectedItem as CultureInfo;
            if (culture != null)
            {
                LocalizedTextEntry newEntry = _textGroup.CreateNew(culture);
                LocalizedTextEntry neutralEntry = (from o in _textGroup
                                                   where o.Culture == LocalizedTextEntry.NeutralCulture.Name
                                                   select o).FirstOrDefault();
                if (neutralEntry != null)
                    newEntry.Value = neutralEntry.Value;
                EntryList.Items.MoveCurrentTo(newEntry);
                EntryText.Focus();
                EntryText.SelectAll();
            }
        }

        private void RemoveCultureButton_Click(object sender, RoutedEventArgs e)
        {
            LocalizedTextEntry entry = EntryList.SelectedItem as LocalizedTextEntry;
            if (entry != null)
            {
                if (entry.Culture == ResourceManager.NeutralLocale)
                {
                    TaskDialog taskDialog = new TaskDialog();
                    taskDialog.Title = EditorApp.Current.MainWindow.Title;
                    taskDialog.Header = "Invalid Operation";
                    taskDialog.Content = String.Format(
                        "You cannot remove the neutral language entry ({0}).",
                        ResourceManager.NeutralCulture.DisplayName);
                    taskDialog.Buttons.Clear();
                    taskDialog.Buttons.Add(new TaskDialogButtonData((int)TaskDialogButtons.OK, "OK", "OK", true));
                    taskDialog.AllowDialogCancellation = true;
                    taskDialog.MainIcon = TaskDialogIconConverter.ConvertFrom(TaskDialogIcon.Error);
                    taskDialog.MaxWidth = 640;
                    taskDialog.Owner = this;
                    taskDialog.Show();
                    return;
                }
                _textGroup.Remove(entry);
            }
        }

        private void EntryList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            EntryText.IsEnabled = (EntryList.SelectedItem != null);
        }
    }
}
